#include "mymessagebox.h"
#include "ui_mymessagebox.h"
#include <QBitmap>
#include <QPainter>
#include <QStyleOption>

MyMessageBox::MyMessageBox(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyMessageBox)
    , result(-1)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    connect(ui->close, &QPushButton::clicked, this, &MyMessageBox::onCloseButtonClicked);
}

MyMessageBox::~MyMessageBox()
{
    delete ui;
}

// 实现窗口拖动功能
void MyMessageBox::mousePressEvent(QMouseEvent *event)
{
    // 检查是否点击了自定义标题栏
    if (event->button() == Qt::LeftButton && event->pos().y() <= 41) {
        m_isTitleBarClicked = true;
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    } else {
        m_isTitleBarClicked = false;
        // 让事件继续传播，确保其他控件能响应点击事件
        QWidget::mousePressEvent(event);
    }
}

void MyMessageBox::mouseMoveEvent(QMouseEvent *event)
{
    // 只有在点击了标题栏之后才允许拖动
    if (m_isTitleBarClicked && (event->buttons() & Qt::LeftButton)) {
        move(event->globalPos() - m_dragPosition);
        event->accept();
    } else {
        // 让事件继续传播，确保其他控件能响应移动事件
        QWidget::mouseMoveEvent(event);
    }
}

void MyMessageBox::setWindowTitle(const QString &title)
{
    ui->title->setText(title);
}

void MyMessageBox::setText(const QString &text)
{
    ui->text->setText(text);
}

QPushButton* MyMessageBox::createButton(const QString &text, int x, int y)
{
    QPushButton *button = new QPushButton(text, this);
    button->setGeometry(x, y, 82, 32);
    button->show();
    buttons.append(button);
    return button;
}

void MyMessageBox::setButton()
{
    createButton("关闭", 320, 184);
}

void MyMessageBox::setButton(const QString &button1)
{
    createButton(button1, 320, 184);
}

void MyMessageBox::setButton(const QString &button1, const QString &button2)
{
    createButton(button1, 220, 184);
    createButton(button2, 320, 184);
}

void MyMessageBox::setButton(const QString &button1, const QString &button2, const QString &button3)
{
    createButton(button1, 120, 184);
    createButton(button2, 220, 184);
    createButton(button3, 320, 184);
}

int MyMessageBox::exec()
{
    this->setWindowModality(Qt::ApplicationModal);
    this->show();

    QEventLoop loop;
    connect(ui->close, &QPushButton::clicked, &loop, &QEventLoop::quit);
    for (int i = 0; i < buttons.size(); ++i) {
        QPushButton* button = buttons[i];
        button->setStyleSheet(buttonStyle);
        connect(button, &QPushButton::clicked, this, &MyMessageBox::onSetButtonClicked);
        connect(button, &QPushButton::clicked, &loop, &QEventLoop::quit);
    }
    loop.exec();

    return result;
}

void MyMessageBox::onSetButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (button) {
        int index = buttons.indexOf(button);
        if (index != -1) {
            result = index;
        }
    }
    this->close();
}

void MyMessageBox::onCloseButtonClicked()
{
    this->close();
}

void MyMessageBox::paintEvent(QPaintEvent *event)
{
    // 1. 绘制样式（QSS）
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); // 绘制样式

    // 2. 设置掩码，约定 painter 绘制区域
    QBitmap bmp(this->size());
    bmp.fill(Qt::white); // 填充 bitmap

    QPainter painter(&bmp);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::black); // 掩码值
    painter.drawRoundedRect(bmp.rect(), 10, 10); // 在 bitmap 中绘制圆角矩形

    // 手动绘制边框
    QPainter borderPainter(this);
    borderPainter.setRenderHint(QPainter::Antialiasing);
    QPen pen(QColor("#55aaff")); // 设置边框颜色
    pen.setWidth(2);              // 设置边框宽度
    borderPainter.setPen(pen);
    borderPainter.setBrush(Qt::NoBrush);
    QRect rect = this->rect();
    rect.adjust(1, 1, -1, -1); // 调整边框矩形，以确保边框在控件内部
    borderPainter.drawRoundedRect(rect, 10, 10); // 绘制圆角边框

    // 设置掩码以确定本控件可绘制区域
    setMask(bmp);

    // 调用基类的 paintEvent
    QWidget::paintEvent(event);
}
